package com.pinfly.common.util;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.log4j.Logger;

/**
 * Provides stack trace utilities
 */
public class StackTrace
{
    private static final Logger logger = Logger.getLogger (StackTrace.class);

    // This class shouldn't be instantiated
    private StackTrace ()
    {
    }

    /**
     * getStack This static method returns a stack trace given the passed in
     * Throwable object.
     * 
     * @param ex The Throwable object to obtain the stack trace from.
     * @return The message and stack trace as a String.
     */
    static public String getStack (Throwable ex)
    {
        String stack = null;
        try
        {
            StringWriter sw = new StringWriter ();
            PrintWriter pw = new PrintWriter (sw);

            ex.printStackTrace (pw);

            stack = sw.toString ();

            pw.close ();

        }
        catch (Exception e)
        {
            logger.fatal ("Failed to convert stack trace to String", e);
        }

        return (stack);
    }

}
